import { vi } from 'vitest';

// Setup global mocks
Object.defineProperty(window, 'localStorage', {
  value: {
    getItem: vi.fn(),
    setItem: vi.fn(),
    removeItem: vi.fn(),
    clear: vi.fn(),
  },
  writable: true,
});

Object.defineProperty(window, 'sessionStorage', {
  value: {
    getItem: vi.fn(),
    setItem: vi.fn(),
    removeItem: vi.fn(),
    clear: vi.fn(),
  },
  writable: true,
});

// Mock console methods to reduce noise in tests
global.console = {
  ...console,
  log: vi.fn(),
  debug: vi.fn(),
  info: vi.fn(),
  warn: vi.fn(),
  error: vi.fn(),
};

// Setup fetch mock
const mockFetch = vi.fn();
global.fetch = mockFetch;

// Setup XMLHttpRequest mock
class MockXMLHttpRequest {
  public readyState = 0;
  public status = 0;
  public statusText = '';
  public responseText = '';
  public response = '';
  public upload = {
    addEventListener: vi.fn(),
    removeEventListener: vi.fn()
  };
  
  public addEventListener = vi.fn();
  public removeEventListener = vi.fn();
  public open = vi.fn();
  public send = vi.fn();
  public setRequestHeader = vi.fn();
  public abort = vi.fn();
  public getAllResponseHeaders = vi.fn().mockReturnValue('');
}

global.XMLHttpRequest = MockXMLHttpRequest as any;

// Export for use in tests
export { mockFetch, MockXMLHttpRequest };